# -*- coding:utf-8 -*-

import sys
from common.import_pyside import *


class DemoMessageBox(QWidget):
    def __init__(self, parent=None):
        super(DemoMessageBox, self).__init__(parent)

        # 设置窗口标题
        self.setWindowTitle('实战PyQt5: QMessageBox Demo!')
        # 设置窗口大小
        self.resize(300, 200)

        self.initUi()

    def initUi(self):
        gLayout = QGridLayout(self)

        # 常规信息
        btnInformation = QPushButton('常规信息', self)
        btnInformation.setMinimumHeight(40)
        btnInformation.clicked.connect(self.onInformationButtonClicked)

        # 警告信息
        btnWarning = QPushButton('警告信息', self)
        btnWarning.setMinimumHeight(40)
        btnWarning.clicked.connect(self.onWarningButtonClicked)

        # 错误信息
        btnCritical = QPushButton('错误信息', self)
        btnCritical.setMinimumHeight(40)
        btnCritical.clicked.connect(self.onCriticalButtonClicked)

        # 提问
        btnQuestion = QPushButton('提问信息', self)
        btnQuestion.setMinimumHeight(40)
        btnQuestion.clicked.connect(self.onQuestionButtonClicked)

        # About
        btnAbout = QPushButton('About', self)
        btnAbout.setMinimumHeight(40)
        btnAbout.clicked.connect(self.onAboutButtonClicked)

        # 自定义消息框
        btnUser = QPushButton('自定义消息框', self)
        btnUser.setMinimumHeight(40)
        btnUser.clicked.connect(self.onUserButtonClicked)

        gLayout.addWidget(btnInformation, 0, 0)
        gLayout.addWidget(btnWarning, 0, 1)
        gLayout.addWidget(btnCritical, 1, 0)
        gLayout.addWidget(btnQuestion, 1, 1)
        gLayout.addWidget(btnAbout, 2, 0)
        gLayout.addWidget(btnUser, 2, 1)

        self.setLayout(gLayout)

    def onInformationButtonClicked(self):
        QMessageBox.information(self, '信息', '这是一个常规信息')

    def onWarningButtonClicked(self):
        QMessageBox.warning(self, '警告', '这是一个警告信息')

    def onCriticalButtonClicked(self):
        QMessageBox.critical(self, '错误', '这是一个错误信息')

    def onQuestionButtonClicked(self):
        reply = QMessageBox.question(self, '退出', '确认退出?', QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel,
                                     QMessageBox.Cancel)
        if reply == QMessageBox.Yes:
            QMessageBox.information(self, '信息', '点击了 YES 按钮')
        elif reply == QMessageBox.No:
            QMessageBox.information(self, '信息', '点击了 No 按钮')
        else:
            QMessageBox.information(self, '信息', '点击了 Cancel 按钮')

    # About 方式，无任何提示图标
    def onAboutButtonClicked(self):
        QMessageBox.about(self, '关于', '关于<font color="blue">实战PyQt5</font>')

    def onUserButtonClicked(self):
        box = QMessageBox(QMessageBox.Question, '退出', '确定退出?')
        yes = box.addButton('确定', QMessageBox.YesRole)
        no = box.addButton('取消', QMessageBox.NoRole)
        box.setIcon(QMessageBox.Warning)
        box.resize(240, 180)
        box.exec()
        if (box.clickedButton() == yes):
            QMessageBox.information(self, '信息', '点击了 确定 按钮')
        else:
            QMessageBox.information(self, '信息', '点击了 取消 按钮')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = DemoMessageBox()
    window.show()
    sys.exit(app.exec())
